using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Windows.Forms;

namespace Schedulr.Helpers
{
    internal static class Logger
    {
        #region DefaultLogFilePath

        /// <summary>
        /// The default path to the log file.
        /// </summary>
        public static string DefaultLogFilePath
        {
            get
            {
                return Path.Combine(Application.StartupPath, "SchedulrActivity.log");
            }
        }

        #endregion

        #region Fields

        /// <summary>
        /// The tracer to log to.
        /// </summary>
        private static Tracer tracer = new Tracer("Schedulr");

        /// <summary>
        /// The synchronisation object to lock on.
        /// </summary>
        private static object lockObject = new object();

        #endregion

        #region Log

        /// <summary>
        /// Logs an informational message.
        /// </summary>
        /// <param name="message">The message to trace.</param>
        public static void Log(string message)
        {
            Log(message, TraceEventType.Information);
        }

        /// <summary>
        /// Logs an exception.
        /// </summary>
        /// <param name="message">The message to trace.</param>
        /// <param name="exc">The exception that occurred.</param>
        public static void Log(string message, Exception exc)
        {
            string errorMessage = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", message, exc.ToString());
            Log(errorMessage, TraceEventType.Error);
        }

        /// <summary>
        /// Logs a message.
        /// </summary>
        /// <param name="message">The message to trace.</param>
        /// <param name="eventType">The type of event that caused the trace.</param>
        public static void Log(string message, TraceEventType eventType)
        {
            lock (lockObject)
            {
                tracer.Trace(string.Format(CultureInfo.CurrentCulture, "{1}[{0}] {2}", DateTime.Now, new string(' ', 11 - eventType.ToString().Length), message), eventType);
            }
        }

        #endregion
    }
}